<?php
namespace app\admin\controller;
use think\facade\Db;
use think\facade\Request;
use think\facade\Filesystem;
class Attachment extends Base{
    function initialize()
    {
        parent::initialize();
    }
    public function index(){
        $list = Db::name('Attachment')->order('id desc')
            ->paginate(14);
        $page = $list->render();
        $view = [
            'list' => $list,
            'page' => $page,
        ];
        return view('', $view);

    }
    public function getList($oldAids=''){
        if($oldAids){
            $list = Db::name('Attachment')->where('id','not in',$oldAids)->order('id desc')->paginate(6);
        }else{
            $list = Db::name('Attachment')->order('id desc')->paginate(6);
        }
        $page = $list->render();
        $view = [
            'list' => $list,
            'page' => $page,
        ];
        if(input('type'==2)){
            return view('get_lists', $view);
        }else{
            return view('', $view);
        }

    }


    public function clear(){
        //获取所以未使用图片链接
        $url1 = Db::name('attachment')->where('count','<=',0)->column('url');
        //删除未使用图片数据
        Db::name('attachment')->where('count','<=',0)->delete();
        //删除未使用图片
        $this->clearUrl($url1);

        //获取所有记录已使用图片的资源ID
        $aid = Db::name('attimg')->column('aid');
        if($aid){
            //获取未使用图片链接
            $url2 = Db::name('attachment')->where([['id','not in',$aid]])->column('url');
            //删除未使用图片数据
            Db::name('Attachment')->where([['id','not in',$aid]])->delete();
            //删除未使用图片
            $this->clearUrl($url2);
        }
        return ['code'=>0,'msg'=>'清除成功！'];
    }
    //删除资源
    function clearUrl($url){
        if($url){
            if(is_array($url)){
                foreach ($url as $k=>$v){
                    if(file_exists('.'.$v)){
                        unlink('.'.$v);
                    }
                }
            }else{
                if(file_exists($url)){
                    unlink($url);
                }
            }
        }
    }
    public function upload(){
        // 获取上传文件表单字段名
        $fileKey = array_keys(request()->file());
        // 获取表单上传文件
        $file = request()->file($fileKey['0']);
        try {
            validate(['image'=>'filesize:10240|fileExt:jpg,png,gif,jpeg'])
                ->check(object2array($file));
            $savename = Filesystem::disk('public')->putFile( 'uploads', $file);

            $result['code'] = 0;
            $result['msg'] = '图片替换成功!';
            $path=str_replace('\\','/',$savename);
            $hex = checkHex(public_path().$path);
            if($hex==1){
                $result['code'] =1;
                $result['msg'] = '请上传合法图片';
                $result['url'] = '';
                return $result;
            }
            $result['url'] = '/'. $path;
            $aid = input('id');
            $ainfo = Db::name('attachment')->where('id',$aid)->find();

            if($ainfo['type']=='config'){
                Db::name('config')->where(['inc_type'=>'system','name'=>$ainfo['title']])->update(['value'=>$result['url']]);
            }else{
                $list = Db::name('attimg')->where('aid',$aid)->select();
                foreach ($list as $k=>$v){
                    if($v['more']==1){
                        $field = Db::name($v['table'])->where(['id'=>$v['fid']])->value($v['field']);
                        $data = json_decode($field,true);
                        $data[$aid] = $result['url'];
                        Db::name($v['table'])->where(['id'=>$v['fid']])->update([$v['field']=>json_encode($data)]);
                    }else{
                        Db::name($v['table'])->where(['id'=>$v['fid']])->update([$v['field']=>$result['url']]);
                    }
                }
            }
            Db::name('attachment')->where('id',$aid)->update(['url'=>$result['url']]);
            $this->clearUrl('.'.$ainfo['url']);
            return $result;
        } catch (think\exception\ValidateException $e) {
            $result['code'] =1;
            $result['msg'] = $e->getMessage();
            $result['url'] = '';
            return $result;
        }
    }

}